package com.sharefang.aproject.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.orhanobut.logger.Logger;
import com.sharefang.aproject.activity.NppPlayActivity;
import com.sharefang.aproject.adapter.XRecyclerViewAdapter;
import com.sharefang.aproject.bean.NppBean;
import com.sharefang.aproject.bean.SelfBean;
import com.sharefang.aproject.constant.CacheKey;
import com.sharefang.aproject.constant.FragmentType;
import com.sharefang.aproject.constant.UserNppType;
import com.sharefang.aproject.net.NetApi;
import com.sharefang.aproject.presenter.GetNppPresenter;
import com.sharefang.aproject.utils.CacheHelper;
import com.sharefang.aproject.utils.UiUtil;
import com.sharefang.aproject.view.INppView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/19.
 */

@SuppressLint("ValidFragment")
public class UserNppFragment extends RecyclerViewFragment implements INppView, NetApi, CacheKey {
    Context context;
    UserNppType nppType;
    GetNppPresenter nppPresenter;
    Map<String, Object> map = new HashMap<>();
    int uploadOffset = -1;
    int abortOffset = -1;
    int favoriteOffset = -1;
    List<NppBean> nppBeens;

    public UserNppFragment(Context context, UserNppType nppType) {
        this.context = context;
        this.nppType = nppType;
        map.put("beginTime", -1);
        map.put("endTime", -1);
        map.put("offset", -1);
        map.put("page", 1);
        map.put("limit", 10);
        switch (this.nppType) {
            case UPLOADED:
                nppPresenter = new GetNppPresenter("", this);
                break;
            case ABORT:
                nppPresenter = new GetNppPresenter(NET_ABORT_NPP, this);
                break;
            case FAVORITE:
                SelfBean selfBean = (SelfBean) CacheHelper.getAsObject(SELF_BEAN);
                if (selfBean != null)
                    map.put("userId", selfBean.getUserId() + "");
                nppPresenter = new GetNppPresenter(NET_FAVORITE_NPP, this);
                break;
        }

        nppPresenter.getNpp(map, false);

    }

    @Override
    void doRefresh() {
        switch (this.nppType) {
            case UPLOADED:
                uploadOffset = -1;
                break;
            case ABORT:
                abortOffset = -1;
                break;
            case FAVORITE:
                favoriteOffset = -1;
                break;
        }

        map.remove("offset");
        map.put("offset", -1);
        nppPresenter.getNpp(map, false);
    }

    @Override
    void doLoadMore() {
        switch (this.nppType) {
            case UPLOADED:
                uploadOffset += 11;
                map.remove("offset");
                map.put("offset", uploadOffset);
                break;
            case ABORT:
                abortOffset += 11;
                map.remove("offset");
                map.put("offset", abortOffset);
                break;
            case FAVORITE:
                favoriteOffset += 6;
                map.remove("offset");
                map.put("offset", favoriteOffset);
                break;
        }

        nppPresenter.getNpp(map, true);
    }

    @Override
    void setLayoutManager() {
        layoutManager = new LinearLayoutManager(context);
    }

    @Override
    void setAdapter() {
        adapter = new XRecyclerViewAdapter(context, FragmentType.USER);
    }

    @Override
    void onItemClick(View view, int position) {
        UiUtil.showToast(context, "点击了" + position);
        Intent intent = new Intent();
        intent.setClass(context, NppPlayActivity.class);
        intent.putExtra("nppBean", nppBeens.get(position));
        startActivity(intent);
    }

    @Override
    void onItemLongClick(View view, int position) {

    }

    @Override
    public void doGetNpp(final List<NppBean> nppBeens, boolean loadMore) {

        if (loadMore) {
            if (nppBeens.size() != 0) {
                this.nppBeens.addAll(nppBeens);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.setNppData(nppBeens);
                        adapter.notifyDataSetChanged();
                        xRecyclerView.loadMoreComplete();
                    }
                }, 500);
            } else {
                xRecyclerView.setIsnomore(true);
            }
        } else {
            //刷新

            if (nppBeens.size() != 0) {
                this.nppBeens = nppBeens;

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Logger.d("============type " + nppType + ", nppBeans = " + nppBeens.get(0).toString());
                        adapter.resetNppData(nppBeens);
                        adapter.notifyDataSetChanged();
                        xRecyclerView.refreshComplete();
                        xRecyclerView.reset();
                    }
                }, 500);
            }
        }
    }

    @Override
    public void showFailedError(String message) {
        UiUtil.snake((Activity) context, message);
        xRecyclerView.reset();
    }
}
